package com.example.xymarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;

import com.example.xymarket.adapter.AddressAdapter;
import com.example.xymarket.myView.StatusBar;
import com.example.xymarket.R;
import com.example.xymarket.httpUtil.UserAddressService;
import com.example.xymarket.po.UserAddress;
import com.example.xymarket.common.MyHandler;

import java.util.List;
import java.util.Map;

public class AddressActivity extends AppCompatActivity {

    AddressAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);
        StatusBar.setStatusBarColor(AddressActivity.this, StatusBar.yellow);

        getMyAddress();
    }

    public void getMyAddress()
    {
        MyHandler myHandler=new MyHandler(this,handler);
        UserAddressService userAddressService=new UserAddressService(myHandler);
        userAddressService.getAddress();

    }
    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                List<UserAddress> list = (List<UserAddress>) msg.obj;
                update(list);
            }

            if (msg.what == 100) {
                getMyAddress();
            }

            if (msg.what == 1001) {
                Intent intent  = new Intent(getApplicationContext(),AddAddressActivity.class);
                Map<String,String>map=(Map<String,String>)msg.obj;
                //创建一个并实例化一个Bundle对象；
                Bundle bundle = new Bundle();
                for(Map.Entry<String, String> entry : map.entrySet()) {
                    bundle.putCharSequence(entry.getKey(), entry.getValue());
                }
                //将Bundle对象添加到Intent对象中
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }
    };

    void update(List<UserAddress> list)
    {
        if(list==null)return;
        ListView listView = (ListView) findViewById(R.id.goods_listView);
        try {
            adapter = new AddressAdapter(list,this,handler);
            listView.setAdapter(adapter);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
        }

    }

    public void toInsertAddress(View view)
    {
        Intent intent = new Intent(getApplicationContext(), AddAddressActivity.class);
        startActivity(intent);overridePendingTransition(0, 0);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            Intent intent=new Intent(getApplicationContext(),MineActivity.class);
            startActivity(intent);
            overridePendingTransition(0, 0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}